﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHCommon;
using System.Data;
using System.Data.SqlClient;
namespace HHWeb.SystemManage.DownLoadCodes
{
    public partial class List : System.Web.UI.Page
    {
        HHBLL.DownloadCodes B = new HHBLL.DownloadCodes();
        Help help = new Help();
        public int code = 1;
        public string HDDowncodeNumber = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (help.GetHttpMethodBool())
                {
                    string message = string.Empty;
                    switch (Request.Form["Command"].ToString())
                    {
                        case "Delete":
                            message = ExeDelete();
                            break;
                        case "UpdateStatus":
                            UpdateStatus();
                            break;
                        default:
                            break;
                    }
                    if (!string.IsNullOrEmpty(message))
                    {
                        if (message.EndsWith("成功！"))
                        {
                            MessageBox.ResponseScript(this.Page, "alert('" + message + "')");
                            Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
                        }
                        else
                            MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    }
                }
                else
                {
                    if (!IsPostBack)
                    {
                        ViewState["NowPage"] = 1;
                        HDDowncodeNumber = Request.QueryString["DowncodeNumber"].ToString();
                        ViewState["Where"] = " DowncodeNumber='"+HDDowncodeNumber+"'";
                        Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
                    }
                }
            }
            catch
            {
                MessageBox.ResponseScript(this.Page,"跳转错误！", "javascript:history.go(-1)");
            }
        }
        public void Bind(int NowPage, int PageSize)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@tbname", SqlDbType.NVarChar,50),
					new SqlParameter("@FieldKey", SqlDbType.NVarChar,1000),
					new SqlParameter("@NowPage", SqlDbType.Int,4),
					new SqlParameter("@PageSize", SqlDbType.Int,4),
					new SqlParameter("@FieldShow", SqlDbType.NVarChar,1000),
					new SqlParameter("@FieldOrder", SqlDbType.NVarChar,1000),
					new SqlParameter("@Where", SqlDbType.NVarChar,1000),
                    new SqlParameter("@Totalcount", SqlDbType.Int,4)};
            parameters[0].Value = "DownloadCodes";
            parameters[1].Value = "ID";
            parameters[2].Value = NowPage;
            parameters[3].Value = PageSize;
            parameters[4].Value = "";
            parameters[5].Value = "AddTime desc";
            parameters[6].Value =ViewState["Where"].ToString();
            parameters[7].Direction = ParameterDirection.Output;
            Repeater1.DataSource = HHDBUtility.DbHelperSQL.RunProcedure("P_Pageing", parameters);
            Repeater1.DataBind();
            AspNetPager1.AlwaysShow = true;
            AspNetPager1.PageSize = PageSize;
            AspNetPager1.RecordCount = Convert.ToInt32(parameters[7].Value);
        }
        //AspNetPager分页事件
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            code = 1;
            ViewState["NowPage"] = e.NewPageIndex;
            Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
        }

        #region //删除数据
        public string ExeDelete()
        {
            if (B.DeleteList(Request.Form["hdDelete"].ToString()))
                return "删除成功！";
            else
                return "删除失败！";
        }
        #endregion

        #region //修改状态
        public void UpdateStatus()
        {
            int ID =Convert.ToInt32(Request.Form["hdDelete"]);
            int Status = Convert.ToInt32(Request.Form["HDStatus"]);
            if (Status == 1)
                Status = 0;
            else
                Status = 1;
            B.UpdateStatusByID(ID, Status);
            Bind(Convert.ToInt32(ViewState["NowPage"]), 15);
        }
        #endregion
    }
}